﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DPS.Prototype.Extension;
namespace DPS.Prototype.Log
{
    public class FileLog:ILog
    {
        private static string logPath = AppDomain.CurrentDomain.BaseDirectory;
        public void Log(string message,string batchId)
        {
            try
            {
                string logFilePath = Path.ChangeExtension(Path.Combine(logPath, batchId),"txt");
                string time = DateTime.Now.ToString("------------------yyyy-MM-dd hh:mm:ss-------------------------") + Environment.NewLine;
                message = time + message + Environment.NewLine;
                using (FileStream fs = new FileStream(logFilePath, FileMode.Append, FileAccess.Write))
                {

                    char[] charDataValue = message.ToCharArray();
                    byte[] byDataValue = new byte[charDataValue.Length];
                    System.Text.Encoder ec = System.Text.Encoding.UTF8.GetEncoder();
                    ec.GetBytes(charDataValue, 0, charDataValue.Length, byDataValue, 0, true);
                    // fs.Seek(0, SeekOrigin.Begin);
                    fs.Write(byDataValue, 0, byDataValue.Length);
                }

            }
            catch { }
        }

        public void Log(object logObj, string batchId)
        {
            try
            {
                Log(logObj.Inspect(), batchId);
            }
            catch { }
        }
    }
}
